package com.gjy.design.single;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-06 10:08:04
 */
public class StaticInnerClassSingleton {

    private StaticInnerClassSingleton() {
        // 防御反射攻击
        if (SingletonHolder.INSTANCE != null) {
            throw new RuntimeException("不允许通过反射创建实例");
        }
    }

    // 静态内部类，当外部类被加载时，内部类不会被加载，实现了懒加载
    private static class SingletonHolder {
        // 静态变量由JVM保证在类加载时只初始化一次，并且是线程安全的
        private static final StaticInnerClassSingleton INSTANCE = new StaticInnerClassSingleton();
    }

    public static StaticInnerClassSingleton getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void doSomething() {
        System.out.println("静态内部类单例正在执行业务...");
    }

}
